package com.yanchangapp.ui.presenter;

import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.response.MailListResponse;
import com.yanchangapp.ui.view.SearchAtView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019/7/16.
 * description:搜索P层
 */
public class SearchAtPresenter extends BasePresenter<SearchAtView> {

    public SearchAtPresenter(BaseActivity context) {
        super(context);
    }

    /**
     * 查询搜索列表P层
     * @param netMailType
     * @param keywords
     * @param currentPage
     */
    public void querySearchList(String netMailType,String keywords,int currentPage){
        HeadApiRetrofit.getInstance().queryMailListInfo(netMailType,currentPage,keywords)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MailListResponse>() {
                    @Override
                    public void onNext(MailListResponse mailListResponse) {
                        if (mailListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().querySearchListSuccess(mailListResponse.getData().getList(),keywords);
                            }
                        }else {
                            if (getView() != null){
                                getView().querySearchListFailure(mailListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().querySearchListFailure("获取搜索列表失败");
                        }
                    }
                });
    }
}
